package com.java.second_goods.controller;

import com.java.second_goods.common.AjaxResult;
import com.java.second_goods.entity.Favorite;
import com.java.second_goods.service.FavoriteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收藏信息
 */
@RestController
@RequestMapping("/api/favorite")
@Tag(name = "收藏管理", description = "收藏相关接口")
public class FavoriteController extends BaseController {

    @Autowired
    private FavoriteService favoriteService;

    /**
     * 获取收藏列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取收藏列表", description = "获取收藏列表")
    public AjaxResult list(Favorite favorite) {
        List<Favorite> list = favoriteService.selectFavoriteList(favorite);
        return success(list);
    }

    /**
     * 获取收藏详细信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取收藏详细信息", description = "根据收藏ID获取收藏详细信息")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return success(favoriteService.selectFavoriteById(id));
    }

    /**
     * 新增收藏
     */
    @PostMapping
    @Operation(summary = "新增收藏", description = "新增收藏")
    public AjaxResult add(@RequestBody Favorite favorite) {
        int rows = favoriteService.insertFavorite(favorite);
        if (rows > 0) {
            return success("收藏成功");
        } else {
            return success("已收藏");
        }
    }

    /**
     * 删除收藏
     */
    @DeleteMapping("/{ids}")
    @Operation(summary = "删除收藏", description = "删除收藏")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(favoriteService.deleteFavoriteByIds(ids));
    }

    /**
     * 获取用户收藏列表
     */
    @GetMapping("/user/{userId}")
    @Operation(summary = "获取用户收藏列表", description = "获取用户收藏列表")
    public AjaxResult getUserFavorites(@PathVariable("userId") Integer userId, @RequestParam(value = "type", required = false) String type) {
        List<Favorite> favorites = favoriteService.selectFavoritesByUserId(userId, type);
        return success(favorites);
    }

    /**
     * 检查是否已收藏
     */
    @GetMapping("/check")
    @Operation(summary = "检查是否已收藏", description = "检查是否已收藏")
    public AjaxResult checkFavorite(@RequestParam("userId") Integer userId,
                                    @RequestParam("itemId") Integer itemId,
                                    @RequestParam("type") String type) {
        Boolean isFavorite = favoriteService.checkFavorite(userId, itemId, type);
        return success(isFavorite);
    }

    /**
     * 取消收藏
     */
    @DeleteMapping("/cancel")
    @Operation(summary = "取消收藏", description = "取消收藏")
    public AjaxResult cancelFavorite(@RequestParam("userId") Integer userId,
                                     @RequestParam("itemId") Integer itemId,
                                     @RequestParam("type") String type) {
        return toAjax(favoriteService.deleteFavorite(userId, itemId, type));
    }
}